#include"Led.h"
//#include "Motor.h"



Led_Typedef led_admin[LED_NUM];


void Led1_On()
{
	GPIO_ResetBits(SYS_LED_PORT,SYS_LED_PIN);
}
void Led1_OFF()
{
	GPIO_SetBits(SYS_LED_PORT,SYS_LED_PIN);
}
void Led2_On()
{
	GPIO_SetBits(LED1_PORT,LED1_PIN);
}
void Led2_OFF()
{
	GPIO_ResetBits(LED1_PORT,LED1_PIN);
}
void Led3_On()
{
	GPIO_SetBits(LED2_PORT,LED2_PIN);
}
void Led3_OFF()
{
	GPIO_ResetBits(LED2_PORT,LED2_PIN);
}
void Led4_On()
{
	GPIO_SetBits(LED3_PORT,LED3_PIN);
}
void Led4_OFF()
{
	GPIO_ResetBits(LED3_PORT,LED3_PIN);
}
void Led5_On()
{
	GPIO_SetBits(LED4_PORT,LED4_PIN);
}
void Led5_OFF()
{
	GPIO_ResetBits(LED4_PORT,LED4_PIN);
}
void Led6_On()
{
	GPIO_SetBits(LED5_PORT,LED5_PIN);
}
void Led6_OFF()
{
	GPIO_ResetBits(LED5_PORT,LED5_PIN);
}
void Led7_On()
{
	GPIO_SetBits(LED6_PORT,LED6_PIN);
}
void Led7_OFF()
{
	GPIO_ResetBits(LED6_PORT,LED6_PIN);
}
void Led8_On()
{
	GPIO_SetBits(LED7_PORT,LED7_PIN);
}
void Led8_OFF()
{
	GPIO_ResetBits(LED7_PORT,LED7_PIN);
}
void Led9_On()
{
	GPIO_SetBits(LED8_PORT,LED8_PIN);

}
void Led9_OFF()
{
	GPIO_ResetBits(LED8_PORT,LED8_PIN);
}

void Led_Init()
{
	u8 i;
	for (i = 0; i < LED_NUM; ++i)
	{
		led_admin[i].id = i;
		led_admin[i].active = 1;
		led_admin[i].times_cnt = 0;
		led_admin[i].times_cycle = FOREVER;
		led_admin[i].flash_cnt = 0;
		led_admin[i].statue = LED_OFF;
		led_admin[i].default_statue = LED_ON;
		led_admin[i].on_func = NULL;
		led_admin[i].off_func = NULL;
		led_admin[i].flash_cnt_set = 0;
	}
	
	led_admin[0].on_func = Led1_On;
	led_admin[0].off_func = Led1_OFF;
	led_admin[1].on_func = Led2_On;
	led_admin[1].off_func = Led2_OFF;
	led_admin[2].on_func = Led3_On;
	led_admin[2].off_func = Led3_OFF;
	led_admin[3].on_func = Led4_On;
	led_admin[3].off_func = Led4_OFF;
	led_admin[4].on_func = Led5_On;
	led_admin[4].off_func = Led5_OFF;
	led_admin[5].on_func = Led6_On;
	led_admin[5].off_func = Led6_OFF;
	led_admin[6].on_func = Led7_On;
	led_admin[6].off_func = Led7_OFF;
	led_admin[7].on_func = Led8_On;
	led_admin[7].off_func = Led8_OFF;
	led_admin[8].on_func = Led9_On;
	led_admin[8].off_func = Led9_OFF;
	Led_Set(HEARTBEAT_BEAT_LED,LED_ON,HEARTBEAT_BEAT_LED_CYCLE);
	
	for (i = 0; i < LED_NUM; ++i)
	{
		led_admin[i].on_func();
	}

}

u8 Led_Set(u8 id,LED_ENUM_TypeDef statue,u32 times_cycle)
{
	if(id >= LED_NUM)
	{
		return FALSE;
	}
	if(statue == LED_OFF)
	{
		led_admin[id].active = 0;
	}
	else
	{
		led_admin[id].active = 1;
		led_admin[id].times_cycle = times_cycle;
		led_admin[id].times_on = times_cycle/2;
	}
	return TRUE;
}

u8 Led_Set_Hz(u8 id,LED_ENUM_TypeDef statue,u32 times_cycle,u8 hz)
{
	if(id >= LED_NUM)
	{
		return FALSE;
	}
	if(statue == LED_OFF)
	{
		led_admin[id].active = 0;
	}
	else
	{
		led_admin[id].active = 1;
		led_admin[id].times_cycle = times_cycle;
		led_admin[id].times_on = times_cycle*hz/100;
	}
	return TRUE;
}


u8 Led_Set_FlashCnt(u8 id,LED_ENUM_TypeDef statue,u32 times_cycle,u32 flash_cnt)
{
	if(id >= LED_NUM)
	{
		return FALSE;
	}
	if(statue == LED_OFF)
	{
		led_admin[id].active = 0;
	}
	else
	{
		led_admin[id].active = 1;
		led_admin[id].times_cycle = times_cycle;
		led_admin[id].flash_cnt_set = flash_cnt;
	}
	return TRUE;
}

void Led_Task()
{
	u8 i;
	for (i = 0; i < LED_NUM; ++i)
	{
		if(led_admin[i].active)
		{
			if(led_admin[i].times_cycle == FOREVER)
			{
				led_admin[i].times_cnt = 0;
			}
			else
			{
				led_admin[i].times_cnt++;
			}
			if(led_admin[i].times_cnt > led_admin[i].times_on)
			{
				//off
				if(led_admin[i].off_func != NULL)
				{
					led_admin[i].off_func();
				}
				led_admin[i].statue = LED_OFF;
			}
			else
			{
				if(led_admin[i].on_func != NULL)
				{
					led_admin[i].on_func();
				}
				led_admin[i].statue = LED_ON;
			}
			if(led_admin[i].times_cnt >= led_admin[i].times_cycle)
			{
				led_admin[i].times_cnt = 0;
				if(led_admin[i].flash_cnt_set != 0)
				{
					led_admin[i].flash_cnt++;
					if(led_admin[i].flash_cnt > led_admin[i].flash_cnt_set)
					{
						led_admin[i].flash_cnt = 0;
						Led_Set(i,led_admin[i].default_statue,FOREVER);
					}
				}
			}
		}
		else
		{
			led_admin[i].times_cnt = 0;
			led_admin[i].flash_cnt = 0;
			if(led_admin[i].off_func != NULL)
			{
				led_admin[i].off_func();
			}
		}
	}
}
